package com.acme.bankapp.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;

import com.acme.bankapp.domain.accounts.Account;
import com.acme.bankapp.domain.accounts.AccountType;
import com.acme.bankapp.domain.accounts.CheckingAccount;
import com.acme.bankapp.domain.accounts.SavingsAccount;

/**
 * 
 * Holds information about the client's Name, Gender and Account.
 * Provides methods to get client info.
 * 
 */
public class Client implements Serializable {

	private TreeMap<Integer, Account> accounts;
	private String name;
	private Gender gender;

	//TODO proper init
	protected int mainAccountId;

	/**
	 * Constructs client by name and gender with no account.
	 * @param name full name.
	 * @param gender desired gender.
	 */
	public Client(String name, Gender gender) {
		this.name = name;
		this.gender = gender;	
		accounts = new TreeMap<Integer, Account>();
	}
	
	/**
	 * Constructs client by name and gender with a specified account.
	 * @param name full name.
	 * @param gender desired gender.
	 * @param account specified account
	 */
	public Client(String name, Gender gender, Account account) {
		this(name, gender);
		addAccount(account);
		account.setClient(this);
		mainAccountId = accounts.firstKey();
	}
	
	public void addAccount(Account account) {
		if (accounts.isEmpty()) {
			setMainAccountId(account.getId());
		}
		accounts.put(account.getId(), account);
	}
	
	/** 
	 * Provides single-line description of form:
	 * Name, Gender: Balance.
	 */
	@Override
	public String toString() {
		StringBuffer result = new StringBuffer("" + name + ", " + gender + ": ");
		for (Account account : accounts.values()) {
			result.append(account.getBalance());
			if (account.getAccountType() == AccountType.CHECKING) {
				result.append(", overdraft: ").append(((CheckingAccount) account).getOverdraft());
			}
			result.append("; ");
		}

		return new String(result);
	}
	
	/**
	 * @return String salutation (ex.: Mr. Bill Gates).
	 */
	public String getSalutation() {
		return "" + gender + " " + getName();
	}
	
	/**
	 * @return Client's name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * @return clients Gender.
	 */
	public Gender getGender() {
		return gender;
	}
	
	/**
	 * @return Accounts object. 
	 */
	public Collection<Account> getAccounts() {
		return accounts.values();
	}
	
	public Account getAccountById(int accountId) {
		System.out.println(accounts);
		return accounts.get(accountId);
	}

	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((gender == null) ? 0 : gender.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Client other = (Client) obj;
		if (gender != other.gender) {
			return false;
		}
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

	public int getMainAccountId() {
		return mainAccountId;
	}
	
	public Account getMainAccount() {
		return accounts.get(getMainAccountId());
	}

	public void setMainAccountId(int mainAccountId) {
		this.mainAccountId = mainAccountId;
	}
	
}
